package google.news;

import db.dao.BaseDao;
import domain.result.Result1;
import domain.task.Task1;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2017/1/28.
 */
public class Test {

    public static void main(String[] args) throws IOException {
        DownLoader downLoader = new DownLoader();
        Pattern resultPattern = Pattern.compile("<div id=\\\"resultStats\\\">.+?([\\d,]{1,}).+?<nobr>", Pattern.DOTALL);
        Pattern companyPattern = Pattern.compile("\\[HOST\\](.+?)&");
        BaseDao baseDao = new BaseDao();
        List<Task1> task1List = baseDao.findWithLimit("from Task1 where done=0", 10000, 0);
        System.out.println("startTime : " + new Date().toLocaleString());
        for (Task1 task1 : task1List) {
            int result = 0;
            String url = task1.getUrl().replace("[HOST]", "https://testguge1.arukascloud.io/search?q=");
            System.out.println("process " + url);
            //[HOST]AAR&safe=strict&tbs=cdr:1,cd_min:2004/1/18,cd_max:2004/1/24&tbm=nws
            // 找不到包含所有搜索字词的结果
            String content = downLoader.download(url);
            Matcher matcher = resultPattern.matcher(content);
            if (matcher.find()) {
                String str = matcher.group(1);
                str = str.replaceAll(",", "");
                result = Integer.parseInt(str);
            } else if (content.contains("未搜到与") && content.contains("相关的新闻")) {
                result = 0;
            } else  {
                System.err.println("被封禁了");
                System.exit(-1);
            }
            String company = "";
            matcher = companyPattern.matcher(task1.getUrl());
            if (matcher.find()) {
                company = matcher.group(1);
            }
            Result1 result1 = new Result1();
            result1.setTaskId(task1.getId());
            result1.setNewsCount(result);
            result1.setPeriod(task1.getPeriod());
            result1.setCompanyName(company);
            baseDao.create(result1);
            task1.setDone(1);
            baseDao.update(task1);
        }
        System.out.println("endTime : " + new Date().toLocaleString());
    }
}
